/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Forgot.java
 *
 * Created on Nov 6, 2011, 2:33:38 PM
 */

package shellstation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Anthony
 */
public class Forgot extends javax.swing.JFrame {
    private String SQ;
    private String SA;
    private String UN;
    private int check;
    Forgot f1;
    /** Creates new form Forgot */
    public Forgot() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SAfield = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        SQfield = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        Cancel = new javax.swing.JButton();
        Ok = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        userField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        SAfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SAfieldActionPerformed(evt);
            }
        });

        SQfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SQfieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Security Answer:");

        jLabel1.setText("Security Question:");

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        Ok.setText("Ok");
        Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkActionPerformed(evt);
            }
        });

        jLabel6.setText("Username:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SQfield, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(SAfield)
                        .addComponent(userField, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(28, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(SQfield, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(SAfield, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2)))))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Ok)
                    .addComponent(Cancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SAfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SAfieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_SAfieldActionPerformed

    private void SQfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SQfieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_SQfieldActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        this.check = 2;
        this.f1.dispose();
        LoginPage l = new LoginPage();
        l.setVisible(true);
        l.getLog(l);


}//GEN-LAST:event_CancelActionPerformed

    private void OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkActionPerformed
        // TODO add your handling code here:
        this.SQ=this.SQfield.getText();
        this.SA=this.SAfield.getText();
        this.UN=this.userField.getText();
        if(this.SQ.isEmpty()||this.SA.isEmpty()||this.UN.isEmpty()) {
            JOptionPane.showMessageDialog( null, "Missing values" );
        }
 else if(!this.SQ.isEmpty()&&!this.SA.isEmpty()&&!this.UN.isEmpty()){
             String userType = null;
             String Squest = null;
             String Sans = null;
             ResultSet rs;
             UserController uc = new UserController();
             rs = uc.getuser1(this.UN);
             try {
                if (rs.next()) {
                    try {
                         Squest = rs.getString("securityQuestion");
                         Sans = rs.getString("securityAnswer");
                    } catch (SQLException ex) {
                        Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if((this.SQ == null ? Squest == null : this.SQ.equals(Squest))
                            && (this.SA == null ? Sans == null : this.SA.equals(Sans))){
                         this.check =3;
                         SQA sqa = new SQA(rs);
                   
                         this.f1.dispose();
                         sqa.setVisible(true);
                         sqa.getsqa(sqa);
                         

                    }
                else if((this.SQ == null ? Squest != null : !this.SQ.equals(Squest))
                            && (this.SA == null ? Sans != null : !this.SA.equals(Sans))){

                    JOptionPane.showMessageDialog( null, "Wrong Security Question and Security Answer" );
                    }
               else if((this.SQ == null ? Squest == null : this.SQ.equals(Squest))
                            && (this.SA == null ? Sans != null : !this.SA.equals(Sans))){
     JOptionPane.showMessageDialog( null, "Wrong Security Answer" );
                     }

               else if((this.SQ == null ? Squest != null : !this.SQ.equals(Squest))
                            && (this.SA == null ? Sans == null : this.SA.equals(Sans))){
                   JOptionPane.showMessageDialog( null, "Wrong Security Question" );
               }
                   

                }
                 else{
                     JOptionPane.showMessageDialog( null, "Username do not exist" );
              }

              
            } catch (SQLException ex) {
                Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
            }

 }
       
}//GEN-LAST:event_OkActionPerformed

    public Forgot getforgot(Forgot f){
        this.f1 = f;
        return this.f1;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Forgot().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JButton Ok;
    private javax.swing.JTextField SAfield;
    private javax.swing.JTextField SQfield;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField userField;
    // End of variables declaration//GEN-END:variables

}
